package com.lyn.tool.mvp.model;

import com.lyn.plugin.PluginHelper;
import com.lyn.plugin.entity.Plugin;
import com.lyn.plugin.utils.LogUtil;
import com.lyn.tool.App;
import com.lyn.tool.mvp.contract.MainContract;
import com.lyn.tool.mvp.other.Result;

import java.io.File;
import java.util.LinkedList;
import java.util.List;


public class MainModel implements MainContract.Model {


    @Override
    public Result init() {
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Result.SUCCESS();
    }

    @Override
    public Result loadPlugin(String apkPath) {
        List<Plugin> pluginList = new LinkedList<>();
        try {
            File pluginFolder = new File(apkPath);
            File[] files = pluginFolder.listFiles();
            for (File file : files) {
                if (!file.isFile()) {
                    continue;
                }
                Plugin plugin = PluginHelper.load(App.context, file);
                if (plugin != null) {
                    pluginList.add(plugin);
                }
            }
            return Result.SUCCESS("加载成功!",pluginList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.FAIL("加载失败!");
    }
}
